<br/><?php 
extract($options);
$modelObj = cri($model); 
$commentModelAlias = $modelObj->actsAs['Comments.Commentable']['commentModel'];
$commentModel = cri($commentModelAlias);
$allowedModelsToPost = $commentModel->actsAs['Users.UserContent']['posterModels'];

$loggedInModel = $session->read('Auth.model');

$allowed = false;
$guest = false;
$captcha = false;
$redirectUrl = 'reviews/view/'.$settings['item_id'];
if($model == 'ListingAsset')
{
	$redirectUrl = '/'.$this->params['url']['url'];
}
if(($loggedInModel) && isset($modelObj->{$loggedInModel}->className))
{
	$allowed = true;
}
elseif($model == 'Review' && cr('ReviewConfig.allow_guest_comment'))
{
	if(in_array('Users.Guest', $allowedModelsToPost))
	{
		$allowed = $guest = true;
		if(cr('ReviewConfig.captha_on_review_guest_comment'))
		{
			$captcha = true;
		}		
	}
}elseif($model == 'ListingAsset' && cr('GeneralConfig.allow_guest_comment_on_gallery'))
{
	if(in_array('Users.Guest', $allowedModelsToPost))
	{
		$allowed = $guest = true;
		if(cr('GeneralConfig.captha_on_gallery_guest_comment'))
		{
			$captcha = true;			
		}		
	}
}

?>
<?php
$form->data = array();
if($session->read($commentModelAlias.'Data'))
{	
	$form->data = $session->read($commentModelAlias.'Data');
}

if($allowed)
{ ?>
	<?php if(!isset($addEffects)){?>
	<h4><?php echo __("Add a comment")?></h4>
	<?php }else{?>
	<?php echo $html->link(__("Add a comment"),'#',array('class'=>"button","onclick"=>"\$('#add_comment').toggle();return false;"));
	}?>	
	<?php	
	echo $form->create($commentModelAlias, array('url'=>$url,'id'=>'add_comment','style'=>isset($addEffects) ? "display:none" : ""));
	if($guest)
	{
		$guestFields = cri('Users.Guest')->getFields('group', 'add');
		foreach($guestFields as $field)
		{
			echo $form->input('Guest.'.$field);
		}			
	}
	echo $form->input($commentModel->alias.'.fake',array('type'=>'hidden'));
	echo $form->inputs(array('fieldset'=>false),null,'add');	
	echo $form->input($commentModel->alias.'.'.Inflector::underscore($modelObj->alias).'_id',array('type'=>'hidden','value'=>$settings['item_id']));
	echo $form->input($commentModel->alias.'.status',array('type'=>'hidden','value'=>$commentStatus));
	if($captcha)
	{
		echo $form->input('captcha', array('type'=>'captcha','class'=>'required'));
	}
	echo $html->link(__('Submit'),'javascript:void(0);',array('class'=>'submitComment button'));
	//echo $form->end(array('label'=>__('Submit'),'class'=>'submitComment'));
}else
{
	echo sprintf(__("You need to %s before you can leave any comment on this page."),$html->link(__('login'),'/users/login'));
}?>
<div class="clear"></div>
<?php 
	echo $javascript->link("/cms/js/jquery.validate.min.js");
	echo $javascript->link("/effects/js/jquery.form.js");
?>
	
<script type='text/javascript'>
$(document).ready(function()
{
	$('#GuestName').parent('div').addClass('required');
	$('#GuestEmail').parent('div').addClass('required');
	$('.submitComment').live('click',function(e)
	{		
		if($("#add_comment").valid() == false)
		{
			return;
		}else
		{
			$('#add_comment').ajaxSubmit(
			{
				success:function(status)
				{
					if(status == '' || status == 'error')
					{
						alert("<?php echo __('Invalid captcha code!')?>");
						$('#<?php echo $commentModelAlias?>Captcha').siblings('img').
						replaceWith('<img src="<?php echo $html->url('/cms/captcha/image/220')?>?rand='+Math.random()+'">');
						return;
					}else
					{
						alert(status);
						newUrl = URL+"<?php echo $redirectUrl;?>";
						window.location = newUrl;
					}
				}		
			
			});
		}
	});		
			
});
</script>